

//
//  WYCMeController.m
//  51还款
//
//  Created by 中企互联 on 2017/10/30.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCMeController.h"
#import "WYCRealnameController.h"
#import "WYCAboutMeController.h"
#import "WYCMeWalletController.h"
#import "WYCSetDissController.h"
#import "WYCMessegeController.h"
#import "WYCHelpController.h"
#import "WYCPersonalCenterController.h"
#import "WYCMeTamController.h"
#import "SetGestureLockViewController.h"
#import "WYCMePhotoModel.h"
#import "XMGVerticalButton.h"
#import "WYCLoginController.h"
#import "WYCShimingController.h"
#import "CQUserCenterCell.h"
#import "CQUserCenterCellModel.h"
#import "WYCRepaymentController.h"
#import "WYCmypolicyViewController.h"
#import "WYCMessegeController.h"
#import "WYCBillModel.h"

@interface WYCMeController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray  *dataSource;
@property(nonatomic,strong)NSArray  *imgtitleSource;

@property(nonatomic,strong)UITableView * tableView;


/** 模型*/
@property(nonatomic,strong)WYCMePhotoModel * MePhotoModel;

/** 头像*/
@property(nonatomic,strong)UIImageView * headImage;
/** 个人中心数据数组*/
@property(nonatomic,strong)NSArray *MArrayData;
/** 个人中心数据图标数组*/
@property(nonatomic,strong)NSArray *MArrayImage;
@property(nonatomic,strong)UILabel *banknm;

@end

@implementation WYCMeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = WYCgrayColor;
    //初始化界面
    [self setupPersonalView];
    
}

//设置字体颜色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;//白色
}

//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//初始化列表
-(void)setupPersonalView
{
    //背景图片
    UIImageView  *bgView =  [[UIImageView  alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 180)];
    bgView.image = [UIImage imageNamed:@"my_bg"];
    bgView.userInteractionEnabled = YES ;
    //头像
    _headImage = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-30, 60, 60, 60 )];
   _headImage.image =[UIImage imageNamed:@"head"];
    _headImage.userInteractionEnabled = YES;
    _headImage.layer.cornerRadius= _headImage.frame.size.width/2;
    _headImage.layer.masksToBounds = YES;
    [bgView addSubview:_headImage];

    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(alterHeadPortrait:)];
    //设置界面
    UIButton  *btnSet = [UIButton buttonWithType:UIButtonTypeCustom];
    btnSet.frame = CGRectMake(ScreenWidth-40, 10, 20, 20);
    [btnSet setBackgroundImage:[UIImage imageNamed:@"set"] forState:UIControlStateNormal];
    [btnSet addTarget:self action:@selector(pressSet) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btnSet];
    //消息中心按钮
    UIButton  *btnMessage = [UIButton buttonWithType:UIButtonTypeCustom];
    btnMessage.frame = CGRectMake(20, 10, 20, 20);
    [btnMessage setBackgroundImage:[UIImage imageNamed:@"ring"] forState:UIControlStateNormal];
    [btnMessage addTarget:self action:@selector(pressMaeeage) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:btnMessage];
    

    //给ImageView添加手势
    [_headImage addGestureRecognizer:singleTap];

    //用户姓名
    UILabel  *nameLb = [[UILabel alloc]initWithFrame:CGRectMake(0, _headImage.frame.size.height+_headImage.frame.origin.y +20, ScreenWidth, 20)];
    nameLb.text = [WYCAccountTool unarchiveuserName];
    nameLb.textColor =[UIColor whiteColor];
    nameLb.textAlignment = NSTextAlignmentCenter;
    nameLb.font =[UIFont systemFontOfSize:18];
    [bgView addSubview:nameLb];
 
    
   
    
    _dataSource  = @[@"实名认证",@"费率信息",@"帮助中心",@"联系我们"];
    _imgtitleSource = @[@"my_one",@"my_three",@"my_four",@"my_six"];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    if (ScreenHeight == 812) {
        _tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        _tableView.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
    }
    self.tableView.scrollEnabled = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableHeaderView = bgView;
    [self.view addSubview:self.tableView];
}
-(void)pressMaeeage
{
    WYCMessegeController  *messageView = [[WYCMessegeController alloc]init];
    [self.navigationController pushViewController:messageView animated:YES];
}
-(void)alterHeadPortrait:(UITapGestureRecognizer *)gesture{
    WYCPersonalCenterController * PersonalCenter = [[WYCPersonalCenterController alloc] init];
    [self.navigationController pushViewController:PersonalCenter animated:NO];
}

-(void)pressSet
{
    //设置
WYCSetDissController * setdiss = [[WYCSetDissController alloc] init];
[self.navigationController pushViewController:setdiss animated:NO];

}


#pragma mark - TableView DataSource Method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   
   
    return 4;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * strID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:strID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(indexPath.row == 1)
    {
        UILabel *lixiLb = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-120, 15, 100, 20)];
        lixiLb.textColor = wycMain;
        lixiLb.text = @"当前费率：0.75";
        lixiLb.font = [UIFont systemFontOfSize:13];
        [cell.contentView addSubview:lixiLb];

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    }

    UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(50, 8, 80, 30)];
    titleLb.tintColor =[UIColor blackColor];
    titleLb.text =[_dataSource objectAtIndex:indexPath.row];
    titleLb.font = [UIFont systemFontOfSize:15];
    [cell.contentView addSubview:titleLb];
    UIImageView *tubiao = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 25)];
    tubiao .image =[UIImage imageNamed:[_imgtitleSource objectAtIndex:indexPath.row]];
    [cell.contentView addSubview:tubiao];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (indexPath.row == 0) {
            //实名认证
            WYCShimingController * shiming = [[WYCShimingController alloc] init];
            [self.navigationController pushViewController:shiming animated:NO];
          
    }
//      else  if (indexPath.row == 1) {
//          //我的钱包
//          WYCMeWalletController * mewallet = [[WYCMeWalletController alloc] init];
//          [self.navigationController pushViewController:mewallet animated:NO];
//        }
    
      else  if (indexPath.row == 2)
        {
            //帮助中心
            WYCHelpController * shiming = [[WYCHelpController alloc] init];
            [self.navigationController pushViewController:shiming animated:NO];
        }
       else if (indexPath.row == 3) {
           UIAlertController *alertController =  [UIAlertController alertControllerWithTitle:@"联系我们" message:@"立即拨打给400-867-5559？" preferredStyle:UIAlertControllerStyleAlert];
           
           //添加取消到UIAlertController中
           UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
           [alertController addAction:cancelAction];
           
           //添加确定到UIAlertController中
           UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               NSLog(@"点击了好的按钮");
               NSString *allString = [NSString stringWithFormat:@"tel:400-867-5559"];
               [[UIApplication sharedApplication] openURL:[NSURL URLWithString:allString]];
               
           }];
           
           [alertController addAction:OKAction];
           
           [self presentViewController:alertController animated:YES completion:nil];
          
        }
        
    
}


- (void)SignoutClick{
    
    NSString * str = [NSString stringWithFormat:@"%@token=%@",userlogout,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"=======%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            WYCLoginController * login = [[WYCLoginController alloc] init];
            [self presentViewController:login animated:NO completion:nil];
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
            [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
            [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]]; //字体颜色
        [SVProgressHUD setBackgroundColor:[UIColor blackColor]];   //背景颜色
    }];
    
}
//获取借记卡接口
-(void)setupbankRequst{
    
    //获取借记卡列表
    NSString *getcardlist = [NSString stringWithFormat:@"%@token=%@",usergetDebitCard,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:getcardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] ==0)
       {
           NSArray * resultArray = responseObject[@"result"];
           _banknm.text = [NSString stringWithFormat:@"%lu",(unsigned long)resultArray.count];
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
        [self.tableView.mj_header endRefreshing];
    }];
}
//获取用户头像
- (void)setupheadRequster{
    
    NSString * str = [NSString stringWithFormat:@"%@token=%@",userPortraitDetail,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:str params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            self.MePhotoModel = [WYCMePhotoModel mj_objectWithKeyValues:dict];
            if (self.MePhotoModel.imgUrl.length == 0){
                _headImage.image = [UIImage imageNamed:@"96"];
                
            }else{
                [_headImage sd_setImageWithURL:[NSURL URLWithString:self.MePhotoModel.imgUrl]];
            }
        }else{
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma make ------>>进入控制器隐藏
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //获取用户头像
    [self setupheadRequster];
    [self setStatusBarBackgroundColor:wycMain];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}
#pragma make ------>>离开控制器显示
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self setStatusBarBackgroundColor:[UIColor clearColor]];

   [self.navigationController setNavigationBarHidden:NO animated:YES];
}


@end
